
import { getInvoicesList, preinvoiceopen, exportInvoice, openinvoice, updateInvoiceType, exportInvoiceList, updatePreInvoice, cancelPreInvoice } from '../../services/drawback/invoice';

import Constants from '../../utils/Constants';
import { message } from "antd";
const R = require('ramda');

export default {
    namespace: 'invoice',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },

        list: [],
        dataSource: {},


    },
    reducers: {

        /**
         * 设置数据列表
         * @param state
         * @param payload
         * @returns {{list, page}}
         */
        setInvoicesList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        },

        setDataSource(state, action) {
            let payload = action.payload;
            return { ...state, dataSource: payload.dataSource };
        },







    },
    effects: {
        /**
         * 分页查询收票列表
         *
         * @param call
         * @param put
         * @param select
         */
        *getInvoicesList({ payload }, { call, put, select }) {

            let page = yield select(state => state.invoice.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(getInvoicesList, {
                contractno: R.isNil(payload) || R.isNil(payload.contractno) ? "" : payload.contractno,
                openinvoicestatus: R.isNil(payload) || R.isNil(payload.status) ? "0" : payload.status,
                page: page
            })

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                let listWithKey = R.isNil(content.list) ? [] : content.list;

                yield put({
                    type: 'setInvoicesList',
                    payload: {
                        list: listWithKey,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 开票
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *openinvoice({ payload }, { call, put, select }) {
            let list = payload.list;
            let orderProductDetails = [];

            if (list != null && list.length > 0) {
                for (let i = 0; i < list.length; i++) {
                    let product = {
                        id: list[i].id,
                        productmodel: list[i].productmodel,
                        invoiceremark: list[i].invoiceremark,
                        zzrate: list[i].zzrate,
                        taxrate: list[i].taxrate,
                    };

                    orderProductDetails.push(product);
                }
            }

            let form = {
                order: { id: payload.orderId, invoiceType: payload.invoiceType },
                orderProductDetails: orderProductDetails,
            };

            //更新发票资料
            const openinvoiceRes = yield call(openinvoice, { form });
            if (openinvoiceRes.data.resultCode === "ok") {
                message.info("开票成功！");
                if (payload.onSuccess) payload.onSuccess();
            }
        },

        /**
         * 导出开票资料
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *exportInvoice({ payload }, { call, put, select }) {
            const res = yield call(exportInvoice, { orderId: payload.orderId, drawerId: payload.drawerId });
        },


        /**
         * 预开票
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *preinvoiceopen({ payload }, { call, put, select }) {
            const res = yield call(preinvoiceopen, payload);
            if (res.data.resultCode === "ok") {
                message.info("预开票成功！");

                if (payload.onSuccess) payload.onSuccess();

            } else {
                if (payload.onFail) payload.onFail();
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新预开票
         */
        *updatePreInvoice({ payload }, { call }) {
            const res = yield call(updatePreInvoice, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("更新预开票成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
                message.error(res.data.errMessage);
            }
        },

        /**
         * 预开票
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *updateInvoiceType({ payload }, { call, put, select }) {
            const res = yield call(updateInvoiceType, payload);
            if (res.data.resultCode === "ok") {
                if (payload.onSuccess) payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 导出开票资料列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *exportInvoiceList({ payload }, { call, put, select }) {
            const res = yield call(exportInvoiceList, { contractno: payload.contractno, openinvoicestatus: payload.openinvoicestatus });
        },

        /**
         * 取消预开票
         */
        *cancelPreInvoice({ payload }, { call }) {
            const res = yield call(cancelPreInvoice, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                message.info("预开票取消成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
                message.error(res.data.errMessage);
            }
        },

    }, subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};